class PhotoSizesController < ApplicationController
  before_filter :require_admin,     :only => [:index, :new,   :create,  :edit,    :update,  :destroy]
  before_filter :find_photo_size,   :only => [:show,  :edit,  :update,  :destroy]
  
  def index
    @photo_sizes = PhotoSize.find(:all)
  end

  def show
  end

  def new
    @photo_size = PhotoSize.new
  end

  def create
    @photo_size = PhotoSize.new(params[:photo_size])
    @photo_size.save!
    flash[:update] = 'The photo size has been added'
    redirect_to settings_url
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'There was a problem adding the photo size. See below.'
    redirect_to settings_url
  end
  
  def edit
  end
  
  def update
    @photo_size.update_attributes!(params[:photo_size])
    flash[:update] = 'The information for that photo size has been updated.'
    redirect_to settings_url
  rescue ActiveRecord::RecordInvalid
    flash[:error] = 'Errors were detected, see below.'
    redirect_to settings_url
  end

  def destroy
    @photo_size.destroy
    flash[:update] = 'The photo size has been removed.'
    redirect_to settings_url
  end

protected
  def require_admin
    if logged_in?
      if !current_user.is_admin?
        flash[:error] = "Must be an administrator to do that, sorry."
        redirect_back_or_default('/')
      end
    else
      flash[:error] = 'You must be logged in to do that'
      redirect_to login_url
    end
  end
  def find_photo_size
    @photo_size = PhotoSize.find params[:id]
  end
end
